package com.asset.salesforce.controller;


import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.asset.salesforce.domain.File;
import com.asset.salesforce.form.Customer;
import com.asset.salesforce.service.FileService;
import com.asset.salesforce.service.UserShiroService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

@Controller
public class CountryController {
//	@Autowired
//	private CountryService countryService;
	@Autowired
	MailSender mailSender;
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	UserShiroService userShiroService;
	@RequestMapping(value="/test" ,method=RequestMethod.GET)
	public String get(HttpServletRequest request ,HttpServletResponse httpServletResponse,HttpSession httpSession,Model model){
//		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//		String toAddress = "dev.ahmed.gamal@gmail.com";
//		simpleMailMessage.setTo(toAddress);
//		simpleMailMessage.setSubject("user registeration");
//		simpleMailMessage.setText("user register successfully");
//		mailSender.send(simpleMailMessage);
		return "test";	
	}
	
	@RequestMapping(value="/test" ,method=RequestMethod.POST)
	public String post(HttpServletRequest request ,HttpServletResponse httpServletResponse,HttpSession httpSession,Model model,
			@RequestParam(value="file" ,required=false)MultipartFile name){
//		Country country = new Country();
//		country.setCountryName(name);
//		country.setCountryNameAr(nameAr);
//		countryService.add(country);
//		model.addAttribute("fi", name);
//		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>"+name);
					File file = new File();
					try {
						file.setData(name.getBytes());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					file.setDateCreated(new Date());
					file.setName(name.getOriginalFilename());
					file.setExtension(name.getContentType());
					file.setType(name.getContentType());
					fileService.addfile(file);
//		File file = fileService.getFileById(1);
//			InputStream stream = new ByteArrayInputStream(file.getData());
//			try {
//				BufferedImage bufferedImage =ImageIO.read(stream);
//				String encodeImage = Base64.encode(file.getData());
//				model.addAttribute("encodeImage", encodeImage);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
		return "test";
		
	}
	
	@RequestMapping(value="/" ,method=RequestMethod.GET)
	public String getparent(HttpServletRequest request ,HttpServletResponse httpServletResponse,HttpSession httpSession,Model model){
	//	model.addAttribute("hello", "HELLO");
		if(userShiroService.getCurrentUser() != null){
			return "redirect:/s/profile.html";
		}else{
			return "redirect:/sales/login";
		}
		
		
	}
	@RequestMapping(value="/form-validation",method=RequestMethod.GET)
	public String getValidationForm(ModelMap model){
			        model.addAttribute("customer", new Customer());
			        return "form-validation";
	
	}
	@RequestMapping(value="/form-validation",method=RequestMethod.POST)
	public String postValidationForm(@Valid @ModelAttribute("customer")Customer customer,
			BindingResult result ){
		System.out.println(">>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<_______=============="+customer.toString());
					System.out.println(">>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<_______=============="+result.getAllErrors().size()); 
					if (result.hasErrors()) {
						return "form-validation";
				      }
				        return "test";
	}
	
}
